import 'package:aichat_flutter_wallet/module_common/widgets/skeleton/dapp_top_item_skeleton.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_util/log/log.dart';
// import 'package:lib_util/log/log.dart';

import '../../../../module_common/router/routers.dart';
import '../../../module_browser/viewmodel/before_browser.dart';
import '../dapp_empty.dart';
import '../dapp_title.dart';
import '../dapp_top_item.dart';

import '../../provider/dapp_provider.dart';
import 'package:provider/provider.dart';

// import 'package:lib_util/log/log.dart';
import 'package:lib_database/src/bean.dart';

class DAppFavoriteComp extends StatefulWidget {
  const DAppFavoriteComp({
    super.key,
  });

  @override
  _DAppFavoriteCompState createState() => _DAppFavoriteCompState();
}

class _DAppFavoriteCompState extends State<DAppFavoriteComp> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Consumer<DAppProvider>(
      builder: (c, model, topWidget) {
        List<DAppBean> compList = [];
        final favoriteDAppList = model!.favoriteDAppList.reversed.toList();
        if (favoriteDAppList.length > 0 && favoriteDAppList.length <= 8) {
          compList = favoriteDAppList;
        }
        if (favoriteDAppList.length > 8) {
          compList = favoriteDAppList.sublist(0, 8);
        }

        return Container(
          padding: const EdgeInsets.only(bottom: 15),
          color: Color.fromRGBO(255, 255, 255, 1),
          // color: Colors.black,
          child: Column(
            children: [
              DAppTitle(
                title: 'Favorites',
                showHotIcon: false,
                showMore: true,
                isLoading: model.isLoading,
                onClick: () {
                  Routes.router!.navigateTo(context, Routes.dAppFavoritesRoute);
                },
              ),
              Column(
                children: [
                  if (!model.isLoading && compList.length > 0)
                    Container(
                        margin: const EdgeInsets.only(
                          left: 20,
                        ),
                        padding: const EdgeInsets.only(top: 8, bottom: 2),
                        height: 80.w,
                        child: ListView.builder(
                            itemCount: compList.length,
                            scrollDirection: Axis.horizontal,
                            itemBuilder: (BuildContext context, int index) {
                              DAppBean item = compList[index];
                              WebSiteInfoItem webSiteInfoItem = WebSiteInfoItem(
                                  websiteImg: item.logo!,
                                  websiteName: item.name!,
                                  websiteUrl: item.url!);
                              return DAppTopiItem(
                                  statement: item.statement ?? '',
                                  onClick: () {
                                    doGotoBrowser(
                                        context: context,
                                        webUrl: item.url!,
                                        isSearchIn: false,
                                        webSiteInfoItem: webSiteInfoItem);
                                  },
                                  title: item.name!,
                                  image: item.logo!);
                            })),
                  if (model.isLoading)
                    Container(
                        margin: const EdgeInsets.only(left: 20, top: 10),
                        padding: const EdgeInsets.only(top: 8, bottom: 2),
                        height: 78.w,
                        child: ListView.builder(
                            itemCount: 5,
                            physics: NeverScrollableScrollPhysics(),
                            scrollDirection: Axis.horizontal,
                            itemBuilder: (BuildContext context, int index) {
                              return DAppTopiItemSkeleton();
                            })),
                  if (!model.isLoading && compList.length == 0)
                    DAppEmpty(title: 'No favorite content add yet')
                ],
              ),
            ],
          ),
        );
      },
    );
  }
}
